package com.ruicar.afs.cloud.batch.frame.control;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.ruicar.afs.cloud.batch.entity.AfsBatchInfo;
import com.ruicar.afs.cloud.batch.enums.StepType;
import com.ruicar.afs.cloud.batch.frame.bean.BatchDateInfo;
import com.ruicar.afs.cloud.batch.service.AfsSysHolidayService;
import com.ruicar.afs.cloud.common.core.util.SpringContextHolder;

public class BatchHelper {
    public static boolean shouldRun(StepType stepType, AfsBatchInfo afsBatchInfo){
        if(afsBatchInfo==null) {
            return false;
        }
        BatchDateInfo batchDateInfo = new BatchDateInfo(DateUtil.parse(afsBatchInfo.getBatchDate(), DatePattern.PURE_DATE_PATTERN),isHoliday(afsBatchInfo.getBatchDate()));
        switch (stepType){
            case NOT_RUN:
                return false;
            case YEAR_END:
                return batchDateInfo.isYearEnd();
            case EVERY_DAY:
                return true;
            case WEEK_LAST:
                return batchDateInfo.isWeekEnd();
            case YEAR_HALF:
                return batchDateInfo.isHalfYear();
            case MONTH_LAST:
                return batchDateInfo.isMonthEnd();
            case YEAR_BEGIN:
                return batchDateInfo.isYearStart();
            case MONTH_FIRST:
                return batchDateInfo.isMonthStart();
            case SESSION_END:
                return batchDateInfo.isSessionEnd();
            case SESSION_BEGIN:
                return batchDateInfo.isSessionBegin();
        }
        return false;
    }

    public static boolean isHoliday(String bhDate){
       return SpringContextHolder.getBean(AfsSysHolidayService.class).isHoliday(bhDate);
    }

    public static String nextWorkDay(String bhDate){
        return SpringContextHolder.getBean(AfsSysHolidayService.class).nextWorkDay(bhDate);
    }
}
